﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using HTML;
namespace ERP.PageUC
{
    public partial class ImportOrder : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtGetBranchImport;
        private DataTable dtGetWarehouseImport;
        private DataTable dtGetRoomImport;
        private DataTable dtGetOrderBuyCode;
        private DataTable dtGetOrderBuyTemp;
        private DataTable dtGetEmployee;
        private DataTable dtGetEmployeeDetail;
        private DataTable dtGetOrder;
        private Import_BLL clsImport = new Import_BLL();
        private Employee_BLL clsEmployee = new Employee_BLL();

        protected string StrAlert = string.Empty;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            txtNgayDatHang.Text = Convert.ToString(DateTime.Now.ToString());
            upDateOrderBuy.Update();
            if (!IsPostBack)
            {
                GetGridOrder();
                GetEmployee();
                GetOrderBuyCode();
                GetBranchImport();
                GetWarehouseImport(int.Parse(drChiNhanh.SelectedValue.ToString()));
                GetRoomImport(int.Parse(drChonKho.SelectedValue.ToString()));
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ClearText();
            OpenDialog("#importorder");
            upDateOrderBuy.Update();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
        protected void btnReload_Click(object sender, EventArgs e)
        {
            GetGridOrder();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < ListViewAllOrder.Items.Count; i++)
                {
                    CheckBox chkList = (CheckBox)ListViewAllOrder.Items[i].FindControl("chkList");
                    if (chkList != null)
                    {
                        if (chkList.Checked)
                        {
                            BLL.Import_BLL.Deletetb_Import(int.Parse(ListViewAllOrder.DataKeys[i].Value.ToString()));
                            if (Page.Master != null)
                                ((Label)Page.Master.FindControl("lbThongBao")).Text = "Huỷ phiếu nhập thành công !";
                                OpenDialog("#thongbao");
                                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                        }
                    }
                }
                GetGridOrder();
            }
            catch(Exception ex)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void btnExcel_Click(object sender, EventArgs e)
        {

        }
        //------------Popup---------
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (drNguoiLapPhieu.SelectedValue.ToString() == "-1")
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn người lập phiếu !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (drChungTuNhap.SelectedValue.ToString() == "-1")
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn chứng từ nhập !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (drChiNhanh.Text == string.Empty)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn chi nhánh !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (drChonKho.Text == string.Empty)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn kho !";
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (drPhongChua.Text == string.Empty)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn phòng chứa !";
                    OpenDialog("#thongbao");
                    ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (ListViewOrderImportTemp.Items.Count < 0)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Vui lòng chọn vật tư từ chứng từ nhập !";
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
            if (drNguoiLapPhieu.SelectedValue.ToString() != "-1" && drChungTuNhap.SelectedValue.ToString() != "-1" && drChiNhanh.Text != string.Empty && drChonKho.Text != string.Empty && drPhongChua.Text != string.Empty && ListViewOrderImportTemp.Items.Count > 0)
            {
                InsertImportOrder();
            }
        }
        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            drNguoiLapPhieu.Items.Clear();
            drNguoiLapPhieu.Items.Add(new ListItem("--Chọn người lập phiếu--", "-1"));
            GetEmployee();

            drChungTuNhap.Items.Clear();
            drChungTuNhap.Items.Add(new ListItem("--Chọn chứng từ nhập--", "-1"));
            GetOrderBuyCode();
        }
        protected void btnAddMaterials_Popup_Click(object sender, EventArgs e)
        {

        }
        //--------Btn-Materials------
        protected void btnSearchUC_Click(object sender, EventArgs e)
        {
        }
        protected void btnReloadUC_Click(object sender, EventArgs e)
        {
        }
        protected void btnAddMaterials_Click(object sender, EventArgs e)
        {

        }
        //---------Dropdownlist----------
        protected void drNguoiLapPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drNguoiLapPhieu.SelectedValue.ToString() != "-1")
            {
                GetEmployeeDetail(int.Parse(drNguoiLapPhieu.SelectedValue.ToString()));
            }
            else
            {
                drNguoiLapPhieu.Items.Clear();
                drNguoiLapPhieu.Items.Add(new ListItem("--Chọn người lập phiếu--", "-1"));
                GetEmployee();
                txtChucVu.Text = string.Empty;
                txtBoPhan.Text = string.Empty;
            }
        }
        protected void drChiNhanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            drChonKho.Items.Clear();
            GetWarehouseImport(int.Parse(drChiNhanh.SelectedValue.ToString()));
        }
        protected void drChonKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            drPhongChua.Items.Clear();
            GetRoomImport(int.Parse(drChonKho.SelectedValue.ToString()));
        }
        protected void drChungTuNhap_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetOrderBuyImportTemp(int.Parse(drChungTuNhap.SelectedValue.ToString()));
        }
        //---------ListView-------------
        protected void ListViewOrderDetailTemp_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
 
        }
        protected void ListViewAllOrder_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Acceept")
            {
                //OpenDialog("#orderimportdetail");
                //upDateOrderImportDetail.Update();
            }
        }
        protected void ListViewAllOrder_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }
        #endregion

        #region[Method]
        protected void GetGridOrder()
        {
            dtGetOrder = clsImport.GetDataOrder();
            if (dtGetOrder != null && dtGetOrder.Rows.Count > 0)
            {
                ListViewAllOrder.DataSource = dtGetOrder;
                ListViewAllOrder.DataBind();
                ListViewAllOrder.Visible = true;
            }
            else
            {
                ListViewAllOrder.Visible = false;
            }
        }
        protected void GetEmployee()
        {
            dtGetEmployee = clsEmployee.GetDataEmloyee();
            if (dtGetEmployee != null && dtGetEmployee.Rows.Count > 0)
            {
                drNguoiLapPhieu.DataSource = dtGetEmployee;
                drNguoiLapPhieu.DataTextField = "FullName";
                drNguoiLapPhieu.DataValueField = "Employee_Id";
                drNguoiLapPhieu.DataBind();
                drNguoiLapPhieu.Visible = true;
            }
            else
            {
                drNguoiLapPhieu.Visible = false;
            }
        }
        protected void GetEmployeeDetail(int id)
        {
            dtGetEmployeeDetail = clsEmployee.GetDataEmloyeeDetail(id);
            if (dtGetEmployeeDetail != null && dtGetEmployeeDetail.Rows.Count > 0)
            {
                txtBoPhan.Text = dtGetEmployeeDetail.Rows[0]["Department_Name"].ToString();
                txtChucVu.Text = dtGetEmployeeDetail.Rows[0]["Job_Name"].ToString();
            }
        }
        protected void GetOrderBuyImportTemp(int id)
        {
            dtGetOrderBuyTemp = clsImport.GetOrderBuyImportTemp(id);
            if (dtGetOrderBuyTemp != null && dtGetOrderBuyTemp.Rows.Count > 0)
            {
                ListViewOrderImportTemp.DataSource = dtGetOrderBuyTemp;
                ListViewOrderImportTemp.DataBind();
                ListViewOrderImportTemp.Visible = true;
            }
            else
            {
                ListViewOrderImportTemp.Visible = false;
            }
        }
        protected void GetOrderBuyCode()
        {
            dtGetOrderBuyCode = clsImport.GetOrderBuyCode();
            if (dtGetOrderBuyCode != null && dtGetOrderBuyCode.Rows.Count > 0)
            {
                drChungTuNhap.DataSource = dtGetOrderBuyCode;
                drChungTuNhap.DataTextField = "Order_Buy_Code";
                drChungTuNhap.DataValueField = "Order_Buy_Id";
                drChungTuNhap.DataBind();
            }
        }
        protected void GetBranchImport()
        {
            dtGetBranchImport = clsImport.GetBranch_Import(1);
            if (dtGetBranchImport != null && dtGetBranchImport.Rows.Count > 0)
            {
                drChiNhanh.DataSource = dtGetBranchImport;
                drChiNhanh.DataTextField = "Branch_Name";
                drChiNhanh.DataValueField = "Branch_Id";
                drChiNhanh.DataBind();
            }
        }
        protected void GetWarehouseImport(int id)
        {
            dtGetWarehouseImport = clsImport.GetWarehouse_Import(id, 1);
            if (dtGetWarehouseImport != null && dtGetWarehouseImport.Rows.Count > 0)
            {
                drChonKho.DataSource = dtGetWarehouseImport;
                drChonKho.DataTextField = "Warehouse_Name";
                drChonKho.DataValueField = "Warehouse_Id";
                drChonKho.DataBind();
            }
        }
        protected void GetRoomImport(int id)
        {
            dtGetRoomImport = clsImport.GetRoom_Import(id, 1);
            if (dtGetRoomImport != null && dtGetRoomImport.Rows.Count > 0)
            {
                drPhongChua.DataSource = dtGetRoomImport;
                drPhongChua.DataTextField = "Room_Name";
                drPhongChua.DataValueField = "Room_Id";
                drPhongChua.DataBind();
            }
        }
        protected void InsertImportOrder()
        {
            try
            {
                for (int i = 0; i < ListViewOrderImportTemp.Items.Count; i++)
                {
                    Label lbID = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbID");
                    Label lbIDMaterials_Id = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbIDMaterials_Id");
                    Label lbOrder_Detail_CK_Percent = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbOrder_Detail_CK_Percent");
                    Label lbOrder_Detail_CK = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbOrder_Detail_CK");
                    Label lbOrder_Detail_VAT_Percent = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbOrder_Detail_VAT_Percent");
                    Label lbOrder_Detail_VAT = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbOrder_Detail_VAT");
                    Label lbOrder_Detail_Quality = (Label)ListViewOrderImportTemp.Items[i].FindControl("lbOrder_Detail_Quality");

                    TextBox txtSoLuong = (TextBox)ListViewOrderImportTemp.Items[i].FindControl("txtSoLuong");
                    TextBox txtDonGia = (TextBox)ListViewOrderImportTemp.Items[i].FindControl("txtDonGia");
                    if (int.Parse(lbOrder_Detail_Quality.Text) == 0 || int.Parse(lbOrder_Detail_Quality.Text) < int.Parse(txtSoLuong.Text))
                    {
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Số lượng mặt hàng không đủ để cung cấp !";
                        OpenDialog("#thongbao");
                        ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else
                    {
                        ET.Import_ET obj = new ET.Import_ET();
                        obj.Order_Buy_Detail_Id = int.Parse(lbID.Text);
                        obj.Materials_Id = int.Parse(lbIDMaterials_Id.Text);
                        obj.Quality = int.Parse(txtSoLuong.Text);
                        obj.Price = float.Parse(txtDonGia.Text.Replace(",", ""));
                        obj.User_Id = int.Parse(drNguoiLapPhieu.SelectedValue.ToString());
                        obj.Status = false;
                        obj.DateBegin = Convert.ToDateTime(DateTime.Now.ToString());
                        obj.Room_Id = int.Parse(drPhongChua.SelectedValue.ToString());
                        BLL.Import_BLL.Inserttb_Import(obj);
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Tạo phiếu nhập thành công !";
                        OpenDialog("#thongbao");
                        ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                        ClearText();
                        GetGridOrder();
                        upDateOrderBuy.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
                OpenDialog("#thongbao");
                ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        #endregion

        #region[ClearText]
        protected void ClearText()
        {
            ListViewOrderImportTemp.Visible = false;
            drNguoiLapPhieu.SelectedIndex = -1;
            txtBoPhan.Text = string.Empty;
            txtChucVu.Text = string.Empty;
            txtDienGiai.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            drChungTuNhap.SelectedIndex = -1;
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion

       
    }
}